package com.wx.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.*;
import com.wx.entity.SysNotepad;
import com.wx.holder.WxUserHolder;
import com.wx.service.SysNotepadService;
import com.wx.utils.SnowFlakeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>Title:      ApiNotepadController. </p>
 * <p>Description  记事本管理</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
 * @since      2021/6/19 14:06
 */
@RestController
@RequestMapping(value = "/api/notepad", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "记事本管理(API)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiNotepadController {

    @Resource
    private SysNotepadService sysNotepadService;

    @Resource
    private WxUserHolder wxUserHolder;

    @PostMapping(value = "/listPage")
    @ApiOperation(httpMethod = "POST", value = "分页查询记事本")
    public Wrapper<PageInfo<SysNotepad>> listPage(@ApiParam(name = "SysNotepad", value = "记事本") @RequestBody PageParam<SysNotepad> pageParam) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        pageParam.getParam().setCreateBy(userInfo.getOpenid());
        List<SysNotepad> listPage = sysNotepadService.listPage(pageParam.getParam());
        PageInfo<SysNotepad> pageInfo = new PageInfo<>(listPage);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }


    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增")
    public Wrapper<Integer> save(@ApiParam(name = "banner", value = "轮播图") @RequestBody @Validated(CreateValidate.class) SysNotepad sysNotepad) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysNotepad.setCreateBy(userInfo.getOpenid());
        sysNotepad.setCreator(userInfo.getNickname());
        sysNotepad.setUpdateBy(userInfo.getOpenid());
        sysNotepad.setUpdater(userInfo.getNickname());
        sysNotepad.setId(SnowFlakeUtil.getStringId());
        int result = sysNotepadService.save(sysNotepad);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }


    @PostMapping(value = "/delete/{id}")
    @ApiOperation(httpMethod = "POST", value = "删除")
    public Wrapper<Integer> delete(@PathVariable("id") String id) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        int result = sysNotepadService.deleteById(id,userInfo.getOpenid(),userInfo.getNickname());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    @PostMapping(value = "/update")
    @ApiOperation(httpMethod = "POST", value = "更新")
    public Wrapper<Integer> update(@RequestBody @Validated(UpdateValidate.class) SysNotepad sysNotepad) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysNotepad.setUpdater(userInfo.getNickname());
        sysNotepad.setUpdateBy(userInfo.getOpenid());
        sysNotepad.setUpdateDate(new Date());
        int result = sysNotepadService.update(sysNotepad);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }
}
